<?php
    /**
    * Class: PagerUtils
    * Description: This class support for pager
    */
    /**
    * The record number to start displaying from
    */
class PagerUtils{

    var $limitstart;
    /**
    * Total records
    */
    var $total;
    /**
    * Number rows to display per page
    */
    var $limit;
    /**
    * Number pages to display horizontal
    */
    var $numpagedisplay;
    /**
    * Link of this page
    */
    var $link;
    /**
    * Resource bundle for pager
    */
    var $pager_first;
    var $pager_last;
    var $pager_next;
    var $pager_num_display;
    var $pager_previous;
    var $pager_page;
    var $pager_pages;
    var $pager_of;
    var $pager_result;
    var $pager_results;
    var $pager_no_record;

    //Private support for processing paging
    var $totalpage;

    var $all;
    /*
    * Constructor
    */
    function PagerUtils($limitstart = 0, $total = 0, $limit = 30, $numpage = 10, $link = ""){
    	global $lang_pager_first, $lang_pager_last, $lang_pager_next, $lang_pager_num_display,
            $lang_pager_page, $lang_pager_pages, $lang_pager_previous, $lang_pager_of,
            $lang_pager_result, $lang_pager_results, $lang_pager_no_record;

		$this->all = $limitstart;
        $this->limitstart = max($limitstart, 0);
        $this->total = intval($total);
        if ($limit == "") $this->limit = 30;
        $this->limit = max($limit, 1);
        $this->numpagedisplay = max($numpage, 1);
        $this->link = $link;

        /**
        * If we have less items then the limit set, then the start we always64
        * start from the beginning
        */
        if ($this->limit > $this->total)
        {
            $this->limitstart = 0;
        }
        if (($this->limit - 1) * $this->limit > $this->total){
            $this->limitstart -= $this->limitstart % $this->limit;
        }

        //Localization

        $this->pager_first = $lang_pager_first;
        $this->pager_last = $lang_pager_last;
        $this->pager_next = $lang_pager_next;
        $this->pager_num_display = $lang_pager_num_display;
        $this->pager_page = $lang_pager_page;
        $this->pager_pages = $lang_pager_pages;
        $this->pager_previous = $lang_pager_previous;
        $this->pager_of = $lang_pager_of;
        $this->pager_result = $lang_pager_result;
        $this->pager_results = $lang_pager_results;
        $this->pager_no_record = $lang_pager_no_record;

        //Calculate total pages
        $this->totalpage = ceil($total / $limit);

    }
    /**
    *  Writes the html for the leafs counter, eg, Page 1 of x
    */
    function writeLeafsCounter(){
        $html = null;
        $page = $this->limitstart + 1;

        if($this->total > 0){
            $html .= $this->pager_page . " " . $page ."  ". $this->pager_of . " " . $this->pager_pages;
        }
        return $html;
    }

    /**
    * Write the pagination counter string, ie. Results 1-10 of 42
    */
    function writePagesCounter()
    {
        $html = null;
        $from_result = $this->limitstart + 1;
        $to_result = $this->limitstart + $this->limit;
        //IF the limit reached before the end of the list
        if ($to_result > $this->total){
            $to_result = $this->total;
        }

        if ($this->total > 0){
            $html .= $this->pager_result . " " . $from_result . " - " . $to_result . " "
                . $this->pager_of . " " . $this->total . "  " . $this->pager_results;
        }else{
            $html .= $this->pager_no_record;
        }

        return $html;
    }

    /**
    * Write the pagination with format: <<First <Prev 1 2 3 4 ... x  Next> Last>>
    * Support for backend (Admin) and frontend(End user)
    * 0 = User
    * 1 = Admin
    */

    function writePagesLinks($type, $limitstartname = "limitstart"){
        $html = null;
        $this_page = ceil(($this->limitstart + 1) / $this->limit);
        $start_loop = (floor(($this_page - 1) / $this->numpagedisplay)) * $this->numpagedisplay +1;
        /*
        if(($this_page % ($this->numpagedisplay+1)) > (int)(3 * $this->numpagedisplay / 4)){
           $start_loop = $start_loop + (int)($this->numpagedisplay / 2);
        }else if(($this_page % ($this->numpagedisplay+1)) < (int)($this->numpagedisplay / 3)){
           $start_loop = $start_loop - (int)($this->numpagedisplay / 2);
        }
        if($start_loop < 1 ){
            $start_loop = 1;
        }
        if($start_loop > $this->totalpage){
            $start_loop = $this->totalpage;
        }
        */
        $stop_loop = $start_loop + $this->numpagedisplay - 1;
        if ($stop_loop > $this->totalpage) $stop_loop = $this->totalpage;

        //Admin
        if ($type == 1){
            //Print first links
            if ($this_page > 1){
                $page = ($this_page - 2) * $this->limit;
                $html .= "\n<a href=\"#beg\" class=\"pagenav\" title=\"".$this->pager_first."\" onclick=\"javascript: document.adminForm." . $limitstartname . ".value = 0; document.adminForm.submit();return false;\"><< ".$this->pager_first."</a>";
                $html .= "\n<a href=\"#prev\" class=\"pagenav\" title=\"".$this->pager_previous."\" onclick=\"javascript: document.adminForm." . $limitstartname . ".value=$page; document.adminForm.submit();return false;\">< ".$this->pager_previous."</a>";

            }else{
                $html .= "\n<span class=\"pagenav\">&lt;&lt; ".$this->pager_first."</span>";
                $html .= "\n<span class=\"pagenav\">&lt; ".$this->pager_previous."</span>";
            }

            //Print all pages link
            for ($i = $start_loop; $i <= $stop_loop; $i ++) {
                $page = ($i -1) * $this->limit;
                if ($i == $this_page) {
                    $html .= "\n<span class=\"pagenav\"> $i </span>";
                } else {
                    $html .= "\n<a href=\"#$i\" class=\"pagenav\" onclick=\"javascript: document.adminForm." . $limitstartname . ".value=$page; document.adminForm.submit();return false;\"><strong>$i</strong></a>";
                }
            }
            //Print end links
            if ($this_page < $this->totalpage) {
                $page = $this_page * $this->limit;
                $end_page = ($this->totalpage -1) * $this->limit;
                $html .= "\n<a href=\"#next\" class=\"pagenav\" title=\"".$this->pager_next."\" onclick=\"javascript: document.adminForm." . $limitstartname . ".value=$page; document.adminForm.submit();return false;\"> ".$this->pager_next." ></a>";
                $html .= "\n<a href=\"#end\" class=\"pagenav\" title=\"".$this->pager_last."\" onclick=\"javascript: document.adminForm." . $limitstartname . ".value=$end_page; document.adminForm.submit();return false;\"> ".$this->pager_last." >></a>";
            } else {
                $html .= "\n<span class=\"pagenav\">".$this->pager_next." &gt;</span>";
                $html .= "\n<span class=\"pagenav\">".$this->pager_last." &gt;&gt;</span>";
            }

        }else if($type == 0){
            /**
            * This page navigation if not in the administrator section
            */
           if ($this_page > 1){
                $page = ($this_page - 2) * $this->limit;
                $html .= "\n<a href=\"".$this->link . "/$limitstartname/0\" class=\"paging_text\" title=\"".$this->pager_first."\">" . $this->pager_first."</a>";
                $html .= "\n<a href=\"".$this->link . "/$limitstartname/$page\" class=\"paging_text\" title=\"".$this->pager_previous."\"> ".$this->pager_previous."</a>";
            }else{
                $html .= "\n<span class=\"paing_text_nolink\">".$this->pager_first."</span>";
                $html .= "\n<span class=\"paing_text_nolink\"> ".$this->pager_previous."</span>";
            }

            //Print all pages link
            for ($i = $start_loop; $i <= $stop_loop; $i ++) {
                $page = ($i -1) * $this->limit;
                if ($i == $this_page) {
                    $html .= "\n<span class=\"paing_number_nolink\"> $i </span>";
                } else {
                    $html .= "\n<a href=\"".$this->link."/$limitstartname/$page\" class=\"paging_number\" ><strong>$i</strong></a>";
                }
            }
            //Print end links
            if ($this_page < $this->totalpage) {
                $page = $this_page * $this->limit;
                $end_page = ($this->totalpage -1) * $this->limit;
                $html .= "\n<a href=\"".$this->link."/$limitstartname/$page\" class=\"paging_text\" title=\"".$this->pager_next."\"> ".$this->pager_next." </a>";
                $html .= "\n<a href=\"".$this->link."/$limitstartname/$end_page\" class=\"paging_text\" title=\"".$this->pager_last."\"> ".$this->pager_last."</a>";
            } else {
                $html .= "\n<span class=\"paing_text_nolink\">".$this->pager_next." </span>";
                $html .= "\n<span class=\"paing_text_nolink\">".$this->pager_last."</span>";
            }
        }else if($type == 2){
            /**
            * This page navigation if not in the administrator section
            */

            //Print all pages link
            for ($i = $start_loop; $i <= $stop_loop; $i ++) {
                $page = ($i -1) * $this->limit;
                if ($i == $this_page && $this->all != -1) {
                    $html .= "\n<span class=\"paing_number_nolink\"> $i </span>";
                } else {
                    $html .= "\n<a href=\"".$this->link."/$limitstartname/$page\" class=\"paging_number\" ><strong>$i</strong></a>";
                }
            }
            //Print end links
            if ($this_page < $this->totalpage) {
                $page = $this_page * $this->limit;
                $end_page = ($this->totalpage -1) * $this->limit;
                $html .= "\n<a href=\"".$this->link."/$limitstartname/$page\" class=\"paging_text\" title=\"".$this->pager_next."\"> ".$this->pager_next." </a>";
            }
            if ($this->all == -1){
            	$html .= "\n... \n<span class=\"paing_number_nolink\">All</span>";
            }else{
            	$html .= "\n... <a href=\"".$this->link . "\" class=\"paging_text\" title=\"All\">All</a>";
            }
        }
    	else if($type == 3){
            /**
            * This page navigation if not in the administrator section
            */

            //Print all pages link
            for ($i = $start_loop; $i <= $stop_loop; $i ++) {
                $page = ($i -1) * $this->limit;
                if ($i == $this_page && $this->all != -1) {
                    $html .= "\n<span class=\"paing_number_nolink\"> $i </span>";
                } else {
                    $html .= "\n<a href=\"".$this->link."/$limitstartname/$page\" class=\"paging_number\" ><strong>$i</strong></a>";
                }
            } 
    		if ($this_page < $this->totalpage) {
                $page = $this_page * $this->limit;
                $end_page = ($this->totalpage -1) * $this->limit;
                $html .= "\n<a href=\"".$this->link."/$limitstartname/$page\" class=\"paging_text\" title=\"".$this->pager_next."\"> ".$this->pager_next." </a>";
            }           
        }
        return $html;
    }

    function getSelectedLimitBox($i){
        if ($this->limit == $i){
            return "selected";
        }
        return null;
    }
    function getLimitBox($type, $limitname = "limit", $step = 5){
        $cbo = null;
        if ($type == 1){
            $cbo .= "<select name=\"" . $limitname . "\" id=\"limit\" class=\"inputbox\" size=\"1\" onChange=\"document.adminForm.submit();\">";
        }else{
            $cbo .= "<select name=\"" . $limitname ."\" id=\"limit\" class=\"inputbox\" size=\"1\" onChange=\"document.location.href=\"" . $this->link ."&amp;" . $limitname . "=this.value\">";
        }
        for($i = $step; $i <= 6 * $step; $i+=$step){
            $cbo .= "<option value=\"$i\"" . $this->getSelectedLimitBox($i) .">$i</option>";
        }
        $cbo .= "<option value=\"50\"" . $this->getSelectedLimitBox(50) .">50</option>
                </select>";

        return $cbo;
    }

    function getListFooter($type, $limitstart_name = "limitstart", $limit_name = "limit", $step = 5 ){
         $html = '<table class="adminlist"><tr><th colspan="3">';
         $html .= $this->writePagesLinks($type, $limitstart_name);
         $html .= '</th></tr><tr>';
         $html .= '<td nowrap="nowrap" width="48%" align="right">'.$this->pager_num_display.'</td>';
         $html .= '<td>'.$this->getLimitBox($type, $limit_name, $step).'</td>';
         $html .= '<td nowrap="nowrap" width="48%" >'.$this->writePagesCounter().'</td>';
         $html .= '</tr></table>';
         return $html;
    }

	/**
	 * Write page link
	 * @param int $type
	 * @param string $limitstartname
	 * @return string
	 */
	function writePagesLinkAjax($functionName = "changePage"){
        $html = null;
        $this_page = ceil(($this->limitstart + 1) / $this->limit);
        $start_loop = (floor(($this_page - 1) / $this->numpagedisplay)) * $this->numpagedisplay +1;
        /*
        if(($this_page % ($this->numpagedisplay+1)) > (int)(3 * $this->numpagedisplay / 4)){
           $start_loop = $start_loop + (int)($this->numpagedisplay / 2);
        }else if(($this_page % ($this->numpagedisplay+1)) < (int)($this->numpagedisplay / 3)){
           $start_loop = $start_loop - (int)($this->numpagedisplay / 2);
        }
        if($start_loop < 1 ){
            $start_loop = 1;
        }
        if($start_loop > $this->totalpage){
            $start_loop = $this->totalpage;
        }
        */
        $stop_loop = $start_loop + $this->numpagedisplay - 1;
        if ($stop_loop > $this->totalpage) $stop_loop = $this->totalpage;
		//Print all pages link
            for ($i = $start_loop; $i <= $stop_loop; $i ++) {
                $page = ($i -1) * $this->limit;
                if ($i == $this_page && $this->all != -1) {
                    $html .= "\n<span class=\"paing_number_nolink\"> $i </span>";
                } else {
                    $html .= "\n<a href=\"javascript:;\" onclick=\"" . $functionName . "($page);\" class=\"paging_number\" ><strong>$i</strong></a>";
                }
            } 
    		if ($this_page < $this->totalpage) {
                $page = $this_page * $this->limit;
                $end_page = ($this->totalpage -1) * $this->limit;
                $html .= "\n<a href=\"javascript:;\" onclick=\"" . $functionName . "($page);\" class=\"paging_text\" title=\"".$this->pager_next."\"> ".$this->pager_next." </a>";
            } 
        return $html;
    }
}
?>